# 首先 import 必要的模块
import pandas as pd
import numpy as np
from xgboost import XGBClassifier
from sklearn.preprocessing import OneHotEncoder

def xgb_model(train_data, y, test_data):
        testXGBClassifier =  XGBClassifier(
                learning_rate =0.1,
                n_estimators=10,                                                    #数值大没关系，cv会自动返回合适的n_estimators
                max_depth=5,
                min_child_weight=1,
                gamma=0,
                subsample=0.8,
                colsample_bytree=0.6,
                colsample_bylevel=0.8,
                objective= 'binary:logistic',
                reg_alpha=0.01,
                reg_lambda=0.02,
                seed=3)

        x = train_data
        testXGBClassifier.fit(x,y)                                                 #训练Xgboost

        X_test_leaves = testXGBClassifier.apply(test_data)                        #相当于用xgb提取五个特征
        X_train_leaves = testXGBClassifier.apply(x)
        X_leaves = np.concatenate((X_train_leaves, X_test_leaves), axis=0)        #把训练集和测试集同时求出叶子结点，相加，没下文了？？？？？？
        xgbenc = OneHotEncoder()
        test=xgbenc.fit_transform(X_leaves)                                       #对上面的0--1编码，进行0--1编码训练
        x_train_xgb_onehot=xgbenc.transform(X_train_leaves).toarray()             #训练集的叶子0----1编码转换, 转成49维特征
        x_test_xgb_onehot=xgbenc.transform(X_test_leaves).toarray()               #测试集的叶子0----1编码转换, 转成49维特征
        xgb_onehotDfforTrain=pd.DataFrame(x_train_xgb_onehot,index=train_data.index)   #输出df形式
        xgb_onehotDfforTest=pd.DataFrame(x_test_xgb_onehot,index=test_data.index)      #输出df形式

        return xgb_onehotDfforTrain, xgb_onehotDfforTest
